package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//所有当前控制器的方法全部返回json数据格式
@RestController
//@ResponseBody
//@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

//    @RequestMapping("/html")
//    public String html(){
//        return "login";
//    }

    @RequestMapping("/show")  //ResultMsg--code
    public User selectOne(HttpServletRequest request, Model model){//HttpServletRequest

        //public String selectOne(HttpServletRequest request, Model model)
        //request.setAttribute("name","value");
        //return "login";请求转发

        //public String selectOne(HttpServletRequest request, Model model)
        //model.addAttribute("obj","123");//向request中存储数据
        //return "login";请求转发

        //public ModelAndView selectOne(HttpServletRequest request)
//        ModelAndView mv=new ModelAndView();
//        mv.setViewName("login");//设置视图名称return "login"
//        mv.addObject("obj2","hello");//向request中存储数据
        //return mv;  请求转发

        //从session里查询登录成功的用户信息  session.getAttribute("sys_user")
        HttpSession session=request.getSession();
        Object obj=session.getAttribute("sys_user");
        if(obj instanceof User){
            User user=(User)obj;
            return user;
        }
        return null;
    }

    @RequestMapping("/check")
    public ResultMsg check(String name){
        //查询前端的注册昵称是否可用
       return userService.checkName(name);
    }
    //在控制层将验证的结果返回到前端页面
    @RequestMapping(value = "/login",method = RequestMethod.POST)
//    @ResponseBody //返回json
    //nolog是否记住密码：是先登录成功，为下一次登录准备是否记住密码
    public ResultMsg login(String name, String pass, String nolog, HttpServletRequest request, HttpServletResponse response){//
        //数据请求参数
        //调用业务层并且验证
      ResultMsg resultMsg = userService.login(response,name,pass,nolog,request);//request
        //将结果通知到前端页面

        return resultMsg;
    }

    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    public ResultMsg regist(User user){
       return userService.regist(user);

    }

    @RequestMapping(value = "/getPass",method = RequestMethod.GET)
    public String regist(String name,HttpServletRequest request){
        //从cookie里取出
        Cookie[] cookies = request.getCookies();
        for (Cookie ck:cookies
             ) {
            //把前端传过来的昵称与cookie中的每个键值 名称相比较，如果一致，说明存储过
            if(ck.getName().equals(name)){
               return ck.getValue();
            }
        }
        return "";

    }
}
